package com.adjy.pathbuilders.objects.button;

import android.graphics.Paint;
import android.graphics.Paint.Style;

import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.commons.FontLoader.Fonts;
import com.adjy.pathbuilders.commons.Util;
import com.adjy.pathbuilders.objects.button.listeners.ButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.Point;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;

/**
 *	TextImageButton class implements a ImageButton with a styled text centered on it<br>
 *	This class (for now) has a fixed implementation of the Style for the Text (color, font, etc)
 */
public class TextImageButton extends ImageButton {

	// why do these variables were static?
	private Paint textPaint;
	private Paint strokePaint;
	private String text = "";
	
	private Point textPos = null;

	/**
	 * Creates a TextImageButton based on the given position and Images
	 * 
	 * @param x
	 *            X - Position
	 * @param y
	 *            Y - Position
	 * @param listener
	 *            ButtonListener to be called when the button is clicked
	 * @param selectedImage
	 *            Selected Image for the Button
	 * @param unselectedImage
	 *            Unselected Image for the Button
	 * @param text
	 *            Text to be rendered
	 */
	public TextImageButton(float x, float y, ButtonListener listener,
			WeakReferenceBitmap selectedImage, WeakReferenceBitmap unselectedImage, String text) {
		super(x, y, listener, selectedImage, unselectedImage);
		this.text = text;
		createPaints();
	}

	public TextImageButton(float x, float y, ButtonListener listener,
			WeakReferenceBitmap selectedImage, WeakReferenceBitmap unselectedImage, String text, Paint textPaint, 
						Paint strokePaint) {
		super(x, y, listener, selectedImage, unselectedImage);
		this.text = text;
		this.textPaint = textPaint;
		this.strokePaint = strokePaint;
	}
	
	private void createPaints() {
		int textSize = FontLoader.getInstance().getFontSize();

		textPaint = new Paint();
		textPaint.setARGB(255, 255, 98, 61);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(textSize);
		textPaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));

		strokePaint = new Paint();
		strokePaint.setARGB(255, 172, 51, 22);
		strokePaint.setAntiAlias(true);
		strokePaint.setStrokeWidth(3);
		strokePaint.setTextSize(textSize);
		strokePaint.setStyle(Style.STROKE);
		strokePaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
	}

	@Override
	public void render(GameCanvas canvas, float timeElapsed) {
		super.render(canvas, timeElapsed);
		
		if (textPos == null)
			textPos = Util.centerTextOnCanvas(canvas.getCanvas(),
					textPaint, x, y, width, height, text);
	    
		
		canvas.drawText(text, (int) textPos.getX(), (int) textPos.getY(), textPaint);

		canvas.drawText(text, (int) textPos.getX(), (int) textPos.getY(), strokePaint);
	}

}